package com.rain.rainspringboot3.order.strategy.impl;// OrderSaveContext.java

import com.rain.rainspringboot3.order.entity.Order;
import com.rain.rainspringboot3.order.enums.OrderTypeEnum;
import com.rain.rainspringboot3.order.strategy.OrderSaveStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderSaveContext {

    private final List<OrderSaveStrategy> strategies;

    @Autowired
    public OrderSaveContext(List<OrderSaveStrategy> strategies) {
        this.strategies = strategies;
    }

    public void save(Order order) {
        OrderSaveStrategy strategy = strategies.stream()
            .filter(s -> s.supports(OrderTypeEnum.getByCode(order.getType())))
            .findFirst()
            .orElseThrow(() -> new IllegalArgumentException("不支持的订单类型: " + order.getType()));

        strategy.save(order);
    }
}